#!/usr/bin/python

import os, re
import imp
from pvegasCodeTemplate import *
import common

DEBUG = False
chunkSize = 100 ## <-- number of func-s in one exec

if DEBUG: print "***"*10
if DEBUG: print "Debug mode ON\nchunkSize = %d\n"%chunkSize

config = common.load_config()

listdir = os.listdir('.')

pat = re.compile('.*func.*c$')
obj_src = [i for i in listdir if pat.match(i)]
#if DEBUG: print("DEBUG: obj_src =", obj_src)
name = obj_src[0].split('_func_')[0]

### compile object files
for src in obj_src:
    Object(src)

### list of *.o-files
#objs = [s[:-1] + 'o' for s in obj_src]

## find all possible groups
func = set()
for i in obj_src:
    func.add(tuple(i[:-2].split('V')[1].split('_E')))
#if DEBUG: print "DEBUG: func =",func

headers = [s[:-1] + 'h' for s in obj_src]
#if DEBUG: print "DEBUG: headers =",headers

env = Environment(  LIBS=['m','pvegas'], 
                    LIBPATH=config.pvegasLIBPATHs, CPPPATH=config.pvegasCPPPATHs,
                    CCFLAGS='-Wno-write-strings',##no CC warnings
                    platform='posix',ENV=os.environ) 

for e in func:
    curName = "V%s_E%s." % (e[0], e[1])
    region = ("0," * int(e[0]) + "1," * int(e[0]))[:-1]
    thisFuncList = sorted([f for f in headers if curName in f])
    maxNumOfExecs = int(len(thisFuncList)/chunkSize)
    if len(thisFuncList) > maxNumOfExecs * chunkSize:
        maxNumOfExecs += 1

    includes, functions = [""]*maxNumOfExecs, [""]*maxNumOfExecs
    objs = [[] for i in range(maxNumOfExecs)]
    for i, fileName in enumerate(thisFuncList):
        fname_part = fileName.split("_func_")[1]
        functions[int(i/chunkSize)] += "f[0] += func_t_%s(k);\n" % fname_part.split('_')[0]
        includes[int(i/chunkSize)] += "#include \"%s\"\n" % fileName
        objs[int(i/chunkSize)] += [fileName[:-2]+".o"]

    for i in range(maxNumOfExecs):
        pvegasFile = name + "_%d_"%i + curName + "c"
        if DEBUG: print "DEBUG: pvegasFile =", pvegasFile
        f = open(pvegasFile, 'w')
        f.write('#define DIMENSION %s' % e[0])
        f.write(corePvegasCodeTemplate.format(
            region=region,
            includes=includes[i],
            functions=functions[i]))
        f.close()
        targetName = pvegasFile[:-2]+".run"
        env.Program(target = targetName, source = [pvegasFile]+objs[i])
